/**
 * @author peter
 * The Framework class manages resources (that lets the user list, create and load files probably with Ajax), 
 * and Files, which are created to hold data tha tis loaded from a resource. 
 */
dojo.provide("bunkai.bk_framework");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.layout.LayoutContainer");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.AccordionContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.Toolbar");
dojo.require("dijit.form.Button");
dojo.require("dijit.Menu");
dojo.require("bunkai.bk_editor");
dojo.require("bunkai.bk_file");
dojo.require("bunkai.bk_resource");
dojo.require("bunkai.bk_resource_examples");
dojo.require("bunkai.bk_resource_gdata");
dojo.require("bunkai.bk_editor_editarea");

dojo.declare("bunkai.bk_framework",  [ dijit._Widget, dijit._Templated],
{
	templatePath:  "templates/bk_framework.html",
	widgetsInTemplate: true,
 	resourcemenu: "", // The dropdown button in which we'll add all resources to be accessed
 	files : [], // List of loaded files from resources. Each file object has 1 or more editors inside it
	resources : [],
	default_editor: "", // The default editor for an opened file
	tabcontainer: "", // id of tabcontainer to create resources under
	editorpane: "",
	
 	constructor: function()
	{
		console.log("bunkai.bk_framework constructor called");		
		// Set the default_editor
		this.default_editor = bunkai.bk_editor_editarea;	
	},
	
	postMixInProperties: function()
	{
		
	},
		
	startup: function()
	{
		console.log("bunkai.bk_framework postMixinProperties called"); 
		this.resourcemenu = dijit.byId(this.id+"_resourcemenu");
		console.log("resourcemenu = "+this.resourcemenu);
		console.dir(this.resourcemenu);
		this.tabcontainer = dijit.byId(this.id+"_resourcetabs");
		this.editorpane = dijit.byId(this.id+"_rightpane");	
		console.log("bunkai.bk_framework subscribing to bk_load_file events");
		dojo.subscribe("bk_load_file", function(arg)	
		{
			this.resourseLoadFile(arg.file_id, arg.data, arg.resource_id);
		});
		// Load resources
		this.addResource(new bunkai.bk_resource_examples(this.tabcontainer));
	},
			
	addResource : function(resource)
	{		
		var rname = resource.getName(); // Will be the resource_id in all references
		this.resources[rname] = resource;
		console.log("bunkai.bk_framework addResource called for resource '"+rname+"' as "+resource);
		// add resource as a choice, so user can choose it from a list and browse in it
		var mi = new dijit.MenuItem({label: rname}, document.createElement('div'));
		console.log("bunkai.bk_framework addResource adding "+mi+" to menu "+this.resourcemenu);
		this.resourcemenu.addChild(mi);
		var self = this;
		dojo.connect(mi, "onClick", function(e)
		{
			self.resourceSelected(rname);
		});
	},
			
	resourceSelected: function(e)
	{
		console.log("bk_framework.resourceSelected called. e is '"+e+"'");
		var r = this.resources[e];
		r.show();
	},
	
	// resource callback functions		
	resourceLoadFile: function(file_id, file_data, resource_id)
	{
			console.log("bunkai.bk_framework resoruceLoadFile called");
			var file = new bk_file(file_id, file_data, resource_id, this.editorpane);
			var ed = new this.default_editor();
			file.addEditor(ed);
	}
 });
 
